package ucas.pin.web.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import ucas.pin.web.domain.Disease;
import ucas.pin.web.domain.Entity;
import ucas.pin.web.domain.MedMaterial;
import ucas.pin.web.domain.Prescription;
import ucas.pin.web.dto.GraphDto;
import ucas.pin.web.dto.NodeDto;
import ucas.pin.web.services.DiseaseService;
import ucas.pin.web.services.MedMaterialService;
//import ucas.pin.web.domain.Movie;
import ucas.pin.web.services.PrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Mark Angrish
 */
@RestController("/search")
public class SearchController {

	final PrescriptionService prescriptionService;
	final DiseaseService diseaseService;
	final MedMaterialService medMaterialService;
	

	@Autowired
	public SearchController(PrescriptionService prescriptionService,DiseaseService diseaseService,MedMaterialService medMaterialService) {
		this.prescriptionService = prescriptionService;
		this.diseaseService = diseaseService;
		this.medMaterialService = medMaterialService;
	}
	
	
	

	@RequestMapping("/keyword")
	public List<NodeDto> search(@RequestParam(value = "limit",required = false) Integer limit,@RequestParam(value = "keyword",required = true) String keyWord) {
		System.out.println("search starting ......."+keyWord);
		List<NodeDto> result= new ArrayList();
		Collection<Prescription> preRes = prescriptionService.searchByKeyWord(keyWord);
		Collection<Disease> desRes = diseaseService.searchByKeyWord(keyWord);
		Collection<MedMaterial> medRes = medMaterialService.searchByKeyWord(keyWord);
		for(Entity entity:preRes) {
			result.add(entity.toNodeDto(0));
		}
		for(Entity entity:medRes) {
			result.add(entity.toNodeDto(1));
		}
		for(Entity entity:desRes) {
			result.add(entity.toNodeDto(2));
		}
		return result;
	}
}
